<?php
namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper,
	Application\Auth\UsuarioAuth;
use Zend\Permissions\Acl\Acl;

class Menu extends AbstractHelper
{
	/**
	 * @var EntityManager
	 */
	protected $em;
	/**
	 * Repository autilizado
	 * @var Doctrine\ORM\EntityRepository
	 */
	protected $repository;
	
	protected $acl;
	protected $perfilFunc;
	protected $usuarioAuth;
 
    
    public function __construct($em)
    {
    	$this->em = $em;
    	$this->usuarioAuth = new UsuarioAuth();
    	$this->repository = $this->em->getRepository('Application\Entity\Funcionalidade');
    	
    }
    	
	public function topMenu()
	{
	    $this->perfilFunc =$this->repository->findByFuncPerfil($this->usuarioAuth->getLoggedUser()->idPerfil);
	    $menu = "<li>";
	    $moduloAnterior="";
		foreach ($this->perfilFunc as $val)
		{
			if($moduloAnterior!=$val['noModulo'])
			{
			    $menu.="<a rel='{$val['noModulo']}' href='/{$val['noModulo']}/{$val['noSubmodulo']}/{$val['noTipoFuncionalidade']}'>{$val['noMenu']}</a>";
			    $moduloAnterior=$val['noModulo'];
			}    
		    
		}
		$menu .="</li>";
		return $menu; 
	}
	
	public function sideMenu()
	{
	   $explode = explode("/", $_SERVER ['REQUEST_URI']);
	   $this->perfilFunc =$this->repository->findByFuncPerfilMod($this->usuarioAuth->getLoggedUser()->idPerfil,$explode[1]);
	   $menuLateral = "";
	   $subModAnterior="";
	   $ulAberto=false;
	   foreach ($this->perfilFunc as $val)
	   {
	       $url="/{$val['noModulo']}/{$val['noSubmodulo']}/{$val['noTipoFuncionalidade']}";
	       if($subModAnterior != $val['noMenuSubmodulo'])
	       {
	           if($ulAberto)
	           {
	               $menuLateral .="</ul>";
	           }
	           if($menuLateral !="")
	           {
	               $menuLateral .="</li>";
	           }
	           $menuLateral .="<li>
	           <a href='#'>
	           <i class='{$val['noImg']}'></i>
	           {$val['noMenuSubmodulo']}
	           <span class='fa arrow'></span>
	           </a>
	           <ul class='nav nav-second-level collapse'>
	           <li>
	           <a href='{$url}'>{$val['noMenuFuncionalidade']}</a>
	           </li>";
	           $subModAnterior = $val['noMenuSubmodulo'];
	           $ulAberto=TRUE;
	       }
	       else
	       {
	           $menuLateral .="
	           <li>
	           <a href='{$url}'>{$val['noMenuFuncionalidade']}</a>
	           </li>";
	       }
	   }
	   if($ulAberto)
	   {
	       $menuLateral .="</ul>";
	   }
	   return $menuLateral;
	}
} 
